<cfsilent><cfset time1=getTickCount()>
<!-----
Near final version. I think the user will be responsible for the server-side code.
We can provide a template but not an all encompassing UDF.
------>

<cfset dbsource="ajax">
<cfset formlist="style,rating,price,atmosphere,handi">
<cfset columnlist="RCuisine,RRating,RPrice,RAtmosphere,RHandi">
<cfset table="restaurant">
<!--------
	Construct SQL Where statements.
--------->
	<cfset WhereVar=ArrayNew(1)>
	<cfset count=0>
	<cfset listcount=0>
	<cfset selectfor="">
<cfloop list="#formList#" index="ListIndex">
	<cfset listcount=listcount+1>
	<cfif IsDefined("FORM.#ListIndex#") AND Trim(Evaluate("Form.#ListIndex#")) neq "" AND Trim(Evaluate("Form.#ListIndex#")) neq "null">
		<cfdump var="#ListIndex#">
			<cfset count=count+1>
		<cfif isNumeric(FORM[ListIndex])>
			<cfset WhereVar[count]="#ListGetAt(columnlist,listcount)# = #FORM[ListIndex]#">
		<cfelse>
			<cfset WhereVar[count]="#ListGetAt(columnlist,listcount)#='#FORM[ListIndex]#'">
		</cfif>
	<cfelse>
		<cfset selectfor=ListAppend(selectfor,ListIndex)>
	</cfif>
</cfloop>
<!------
	The URL variable "type" will be set to:
	1. "update" when the request is for updated query counts
	on the select boxes in question.
	
	2. "search" when the actual
	search data is requested.
-------->
</cfsilent><cfif URL.type eq "search"><cfsilent>
	<cfset SelectVar="RName,RCity,RState,RAddress,RCuisine,RZip,RPrice,RRating">
	<cfquery name="result" datasource="#dbsource#">
		Select #SelectVar#
		FROM #table#
		WHERE <cfset mycount=0><cfloop from="1" to="#ArrayLen(WhereVar)#" index="ArrayIndex"><cfset mycount=mycount+1>
		#WhereVar[ArrayIndex]# <cfif mycount LT ArrayLen(WhereVar)> AND</cfif></cfloop>
	</cfquery>
	</cfsilent><cfcontent reset="true" type="text/xml; charset=utf-16"><?xml version="1.0" encoding="UTF-8"?>
<result>
	<table>
		<cfoutput query="result">
			<tr><td>#RName#</td><td>#RCity#</td><td>#RState#</td></tr>
		</cfoutput>
	</table>
</result>
<cfelse>
<!-------------------->
<cfcontent reset="true" type="text/xml; charset=utf-8"><?xml version="1.0" encoding="UTF-8"?><cfset totalRecords = 0>
	<cfoutput>
		<result>
	<cfloop list="#selectfor#" index="myListIndex">
	<cfset SelectVar=ListGetAt(columnlist,ListFind(formlist, myListIndex))>
		<cfquery name="XMLquery" datasource="#dbsource#">
		  Select Distinct #SelectVar#
	          FROM #table#
	        <cfif #ArrayLen(WhereVar)# GT 0> WHERE </CFIF>
	
			<cfset mycount=0><cfloop from="1" to="#ArrayLen(WhereVar)#" index="ArrayIndex"><cfset mycount=mycount+1><cfset thiswhere=WhereVar[ArrayIndex]>
			#PreserveSingleQuotes(thiswhere)# <cfif mycount LT ArrayLen(WhereVar)> AND</cfif>
	            </cfloop>
	            Order By #SelectVar# ASC
		</cfquery>
		<formelement name="#myListIndex#">
				<option>null</option>
			<cfloop query="XMLquery">
				<option>#Evaluate("#SelectVar#")#</option>
			</cfloop>
		</formelement><cfset totalRecords=totalRecords+#XMLquery.recordcount#>
	</cfloop>
		<totalRecords>
			#totalRecords#
		</totalRecords>
		<processingtime purpose="debugging">
			<cfset time2=getTickCount()>#Evaluate(time2-time1)#ms
		</processingtime>
	</result>
</cfoutput>
</cfif>
